package com.xhwl.logistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.logistics.bo.VehiclePassageBO;
import com.xhwl.logistics.bo.cent.ProjectBO;
import com.xhwl.logistics.bo.input.PageBO;
import com.xhwl.logistics.bo.input.QueryVehiclePassageBO;
import com.xhwl.logistics.converter.ToPage;
import com.xhwl.logistics.converter.mapstruct.VehiclePassageConverter;
import com.xhwl.logistics.dao.mapper.IVehiclePassageMapper;
import com.xhwl.logistics.entity.VehiclePassage;
import com.xhwl.logistics.po.VehiclePassagePO;
import com.xhwl.logistics.service.IVehiclePassageService;
import com.xhwl.logistics.service.cent.QueryProjectService;
import com.xhwl.logistics.util.EscapeCharacter;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

//@Service
public class VehiclePassageServiceImpl extends ServiceImpl<IVehiclePassageMapper, VehiclePassage> implements IVehiclePassageService {

    @Autowired
    private IVehiclePassageMapper vehiclePassageMapper;

    @Autowired
    private QueryProjectService queryProjectService;

    @Autowired
    private VehiclePassageConverter vehiclePassageConverter;

    @Autowired
    private ToPage toPage;

    @Override
    public IPage<VehiclePassageBO> queryPassage(QueryVehiclePassageBO queryVehiclePassageBO, PageBO pageBO) {

        /**
         * 对模糊查询字段通道名称字段进行判断操作，防止sql注入
         */
        queryVehiclePassageBO.setPassageName(EscapeCharacter.escape(queryVehiclePassageBO.getPassageName()));
        IPage<VehiclePassagePO> vehiclePassagePOIPage = vehiclePassageMapper.pagePassage(pageBO.toPage(), queryVehiclePassageBO);

        List<ProjectBO> projectBOList = queryProjectService.queryProjects(queryVehiclePassageBO.getProjectIdList());

        List<VehiclePassagePO> vehiclePassagePOList = vehiclePassagePOIPage.getRecords();
        List<VehiclePassageBO> vehiclePassageBOList = vehiclePassagePOList.stream().map(vehiclePassagePO -> {
            Integer projectId = vehiclePassagePO.getProjectId();
            ProjectBO project = projectBOList.stream()
                    .filter(projectBO -> projectId.equals(projectBO.getProjectId()))
                    .findAny()
                    .orElseThrow(() -> new IllegalStateException("查询出的车场id找不到相应的相应的项目"));
            return vehiclePassageConverter.toVehiclePassageBO(vehiclePassagePO, project.getProjectName());
        }).collect(Collectors.toList());

        return toPage.toPage(vehiclePassagePOIPage, vehiclePassageBOList);
    }

    /**
     * 通过项目id查询通道信息
     */
    @Override
    public List<VehiclePassage> queryPassageMessage(Integer projectId) {
        if(projectId==null){
            throw new BusinessException("缺少项目id参数");
        }
        return this.list(conditionForPassage(projectId));
    }

    private Wrapper<VehiclePassage> conditionForPassage(Integer projectId){
        LambdaQueryWrapper<VehiclePassage> vehiclePassageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        return vehiclePassageLambdaQueryWrapper.eq(VehiclePassage::getProjectId,projectId);
    }
}
